using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.Storage;
using MicroMsg.UI.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Resources;
using System.Windows.Threading;
namespace MicroMsg.UI.UserContrl
{
	public class CustomSmileyItem : BaseChatElement
	{
		private const string TAG = "ChatImgItem";
		private static int maxAliveGif = AppInfoHelper.Is256MbDevice() ? 1 : 3;
		private Image image_0;
		private GifControl gifControl_0;
		private Grid imageLayout;
		private TextBlock LoadingInfo;
		private EmojiDownloadInfo emojiInfo;
		private EventWatcher mDownloadWatcher;
		private ChatPageCtrl chatpage;
		private bool isGame;
		private DispatcherTimer playTimer;
		private BitmapImage[] gamePics;
		private int gamePlayCount;
		private string gamePicPath;
		private Image gameImg;
		private int gameMaxPlayTimes = 8;
		private int gamePlayInterval = 300;
		private string mImgSrc;
		private int mProgress = 100;
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			this.chatpage = ChatContainerPage.getCuChatPage();
			CustomSmileyContent customSmileyContent = base.GetTemplateChild("ChatContentCtrl") as CustomSmileyContent;
			this.imageLayout = (base.GetTemplateChild("imageLayout") as Grid);
			this.LoadingInfo = (base.GetTemplateChild("LoadingInfo") as TextBlock);
			customSmileyContent.add_Click(new RoutedEventHandler(this.ContentPanel_Click));
			base.add_Loaded(new RoutedEventHandler(this.onLoaded));
			base.add_Unloaded(new RoutedEventHandler(this.onUnloaded));
			this.RefreshProgress();
			this.RefreshImgSrc();
			this.handlePageStartComplete();
		}
		private void onLoaded(object sender, RoutedEventArgs e)
		{
			Log.smethod_5("ChatImgItem", "CustomSmileyItem onLoaded");
			if (this.gifControl_0 != null)
			{
				try
				{
					this.gifControl_0.Start();
				}
				catch (Exception)
				{
					Log.smethod_1("ChatImgItem", e.ToString());
				}
			}
		}
		private void onUnloaded(object sender, RoutedEventArgs e)
		{
			this.UnitWatcher();
			if (this.gifControl_0 != null)
			{
				try
				{
					this.gifControl_0.Stop();
				}
				catch (Exception)
				{
					Log.smethod_1("ChatImgItem", e.ToString());
				}
			}
		}
		private void InitWatcher()
		{
			if (this.mDownloadWatcher == null)
			{
				this.mDownloadWatcher = new EventWatcher(this, this, new EventHandlerDelegate(this.OnDownloadComplete));
				EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_DOWNLOADIMGD_EMJI_COMPLETE, this.mDownloadWatcher);
			}
		}
		private void UnitWatcher()
		{
			if (this.mDownloadWatcher != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_DOWNLOADIMGD_EMJI_COMPLETE, this.mDownloadWatcher);
				this.mDownloadWatcher = null;
			}
		}
		private void OnDownloadComplete(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			ChatMsg chatMsg = evtArgs.mObject as ChatMsg;
			if (chatMsg != null && chatMsg.nMsgLocalID == this.chatMsg_0.nMsgLocalID)
			{
				this.chatMsg_0 = chatMsg;
				this.showCustomSmiley();
			}
		}
		private void ContentPanel_Click(object sender, RoutedEventArgs e)
		{
			if (this.mclickEvent != null)
			{
				this.mclickEvent(this.chatMsg_0, this);
			}
			if (this.isGame)
			{
				return;
			}
			if (this.chatMsg_0 == null)
			{
				return;
			}
			if (this.chatMsg_0.GetStatus() == MsgUIStatus.Processing)
			{
				MessageBoxManager.ShowToast(strings.Chat_Img_Send_Alert, 2500, null, false);
				return;
			}
			ToolFunction.GotoPageForUserControl("/Source/UI/Chat/GifView.xaml", new object[]
			{
				this.chatMsg_0
			}, false);
		}
		private void RefreshProgress()
		{
		}
		private void RefreshImgSrc()
		{
			if (this.imageLayout == null || this.chatMsg_0 == null)
			{
				return;
			}
			this.emojiInfo = EmojiImageService.getEmojiDownloadInfo(this.chatMsg_0.strMsg);
			if (this.emojiInfo == null)
			{
				this.SetEmptyImage();
				return;
			}
			this.isGame = (this.emojiInfo.nGameExtType > 0);
			if (this.isGame)
			{
				this.showGame();
				return;
			}
			if (!string.IsNullOrEmpty(this.chatMsg_0.strThumbnail) && !string.IsNullOrEmpty(this.chatMsg_0.strPath))
			{
				this.showCustomSmiley();
			}
			else
			{
				if (!string.IsNullOrEmpty(this.emojiInfo.strMd5))
				{
					CustomSmiley customSmileyByMd = CustomSmileyManager.GetCustomSmileyByMd5(this.emojiInfo.strMd5);
					if (customSmileyByMd != null)
					{
						this.chatMsg_0.strThumbnail = customSmileyByMd.ThumbSrc;
						this.chatMsg_0.strPath = customSmileyByMd.ImageSrc;
						this.showCustomSmiley();
						return;
					}
					string text = StorageIO.getImagePath() + "/" + this.emojiInfo.strMd5;
					if (StorageIO.fileExists(text))
					{
						this.chatMsg_0.strThumbnail = text;
						this.chatMsg_0.strPath = text;
						this.showCustomSmiley();
						return;
					}
					this.SetEmptyImage();
					this.InitWatcher();
					new NetSceneEmojiDownload().doScene(this.emojiInfo, this.chatMsg_0);
					return;
				}
			}
		}
		private void showGame()
		{
			bool isSender = this.chatMsg_0.IsSender();
			if (this.emojiInfo.nGameExtType == 1)
			{
				this.gamePicPath = CustomSmileyManager.GetJSBPic(Convert.ToInt32(this.emojiInfo.strGameExtContent), isSender);
			}
			else
			{
				this.gamePicPath = CustomSmileyManager.GetGamePic(Convert.ToInt32(this.emojiInfo.strGameExtContent));
			}
			this.image_0 = new Image();
			BitmapImage bitmapImage = ImageCacheMgr.loadImage(this.gamePicPath, ImageCacheType.MsgImg);
			this.image_0.set_Source(bitmapImage);
			this.image_0.set_Stretch(0);
			this.image_0.set_Width((double)bitmapImage.get_PixelWidth());
			this.image_0.set_Height((double)bitmapImage.get_PixelHeight());
			this.image_0.set_MaxWidth(240.0);
			this.image_0.set_MaxHeight(240.0);
			if (this.chatMsg_0.isGifRead)
			{
				this.imageLayout.get_Children().Add(this.image_0);
				return;
			}
			if (this.emojiInfo.nGameExtType == 1)
			{
				this.gamePics = new BitmapImage[3];
				this.gamePics[0] = ImageCacheMgr.loadImage(CustomSmileyManager.GetJSBPic(1, isSender), ImageCacheType.MsgImg);
				this.gamePics[1] = ImageCacheMgr.loadImage(CustomSmileyManager.GetJSBPic(2, isSender), ImageCacheType.MsgImg);
				this.gamePics[2] = ImageCacheMgr.loadImage(CustomSmileyManager.GetJSBPic(3, isSender), ImageCacheType.MsgImg);
			}
			else
			{
				if (this.emojiInfo.nGameExtType == 2)
				{
					this.gamePics = new BitmapImage[4];
					this.gamePics[0] = ImageCacheMgr.loadImage("Resource/Img/CustomSmiley/dice_action_1.png", ImageCacheType.MsgImg);
					this.gamePics[1] = ImageCacheMgr.loadImage("Resource/Img/CustomSmiley/dice_action_2.png", ImageCacheType.MsgImg);
					this.gamePics[2] = ImageCacheMgr.loadImage("Resource/Img/CustomSmiley/dice_action_3.png", ImageCacheType.MsgImg);
					this.gamePics[3] = ImageCacheMgr.loadImage("Resource/Img/CustomSmiley/dice_action_4.png", ImageCacheType.MsgImg);
				}
			}
			this.gameImg = new Image();
			this.gameImg.set_Source(this.gamePics[0]);
			this.gameImg.set_Stretch(0);
			this.gameImg.set_Width((double)bitmapImage.get_PixelWidth());
			this.gameImg.set_Height((double)bitmapImage.get_PixelHeight());
			this.gameImg.set_MaxWidth(240.0);
			this.gameImg.set_MaxHeight(240.0);
			this.imageLayout.get_Children().Add(this.gameImg);
			this.chatMsg_0.isGifRead = true;
			StorageMgr.chatMsg.modifyMsg(this.chatMsg_0);
			if (this.playTimer == null)
			{
				if (this.emojiInfo.nGameExtType == 2)
				{
					this.gameMaxPlayTimes = 15;
					this.gamePlayInterval = 110;
				}
				this.gamePlayCount = 0;
				this.playTimer = new DispatcherTimer();
				this.playTimer.set_Interval(TimeSpan.FromMilliseconds((double)this.gamePlayInterval));
				this.playTimer.add_Tick(new EventHandler(this.OnPlayGame));
				this.playTimer.Start();
			}
		}
		private void OnPlayGame(object sender, EventArgs e)
		{
			if (this.isGame && this.gamePlayCount >= this.gameMaxPlayTimes && this.playTimer != null)
			{
				this.playTimer.Stop();
				this.playTimer = null;
				this.gamePics = null;
				this.gameImg = null;
				this.imageLayout.get_Children().Clear();
				this.imageLayout.get_Children().Add(this.image_0);
				return;
			}
			this.gamePlayCount++;
			if (this.gameImg != null && this.gamePics != null)
			{
				BitmapImage source = this.gamePics[this.gamePlayCount % this.gamePics.Length];
				this.gameImg.set_Source(source);
			}
		}
		private void showCustomSmiley()
		{
			if (!string.IsNullOrEmpty(this.chatMsg_0.strThumbnail) && !string.IsNullOrEmpty(this.chatMsg_0.strPath))
			{
				if (this.imageLayout.get_Children().get_Count() > 0)
				{
					this.imageLayout.get_Children().Clear();
				}
				this.gifControl_0 = new GifControl();
				this.imageLayout.get_Children().Add(this.gifControl_0);
				this.gifControl_0.Cover = this.chatMsg_0.strThumbnail;
				bool flag = CustomSmileyItem.isGifPic(this.chatMsg_0.strPath);
				if (!this.chatMsg_0.isGifRead)
				{
					this.chatMsg_0.isGifRead = true;
					StorageMgr.chatMsg.modifyMsg(this.chatMsg_0);
					if (flag)
					{
						if (this.chatpage != null && !this.chatpage.IsStartCompleted)
						{
							if ((long)this.chatMsg_0.nMsgLocalID > this.chatpage.LastGifTime)
							{
								this.chatpage.LastGifTime = (long)this.chatMsg_0.nMsgLocalID;
							}
							this.chatpage.playingGifList.Add(this);
							if (this.chatpage.playingGifList.get_Count() > CustomSmileyItem.maxAliveGif)
							{
								this.chatpage.playingGifList.RemoveAt(0);
								return;
							}
						}
						else
						{
							if ((long)this.chatMsg_0.nMsgLocalID >= this.chatpage.LastGifTime)
							{
								if ((long)this.chatMsg_0.nMsgLocalID > this.chatpage.LastGifTime)
								{
									this.chatpage.LastGifTime = (long)this.chatMsg_0.nMsgLocalID;
								}
								this.gifControl_0.Source = this.chatMsg_0.strPath;
								this.handlerAliveGifs();
								return;
							}
						}
					}
				}
				else
				{
					if (flag && this.chatpage != null && (long)this.chatMsg_0.nMsgLocalID > this.chatpage.LastGifTime)
					{
						this.chatpage.LastGifTime = (long)this.chatMsg_0.nMsgLocalID;
					}
				}
				return;
			}
			Log.smethod_1("ChatImgItem", "RefreshImgSrc:GetImageFromPath==null,mImgSrc:" + this.mImgSrc);
			this.SetEmptyImage();
		}
		private void handlerAliveGifs()
		{
			if (this.chatpage != null)
			{
				this.chatpage.playingGifList.Add(this);
				while (this.chatpage.playingGifList.get_Count() > CustomSmileyItem.maxAliveGif)
				{
					this.chatpage.playingGifList.get_Item(0).showStaticImage();
					this.chatpage.playingGifList.RemoveAt(0);
				}
			}
		}
		private void handlePageStartComplete()
		{
			if (this.chatpage != null && this.chatpage.allCustomSmileyIds != null && !this.chatpage.IsStartCompleted && this.chatpage.allCustomSmileyIds.get_Count() > 0 && this.chatpage.allCustomSmileyIds.Contains(this.chatMsg_0.nMsgLocalID))
			{
				this.chatpage.allCustomSmileyIds.Remove(this.chatMsg_0.nMsgLocalID);
				if (this.chatpage.allCustomSmileyIds.get_Count() == 0)
				{
					this.chatpage.IsStartCompleted = true;
					using (List<CustomSmileyItem>.Enumerator enumerator = this.chatpage.playingGifList.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							CustomSmileyItem current = enumerator.get_Current();
							current.showGifImage();
						}
					}
				}
			}
		}
		private void image2_LoadingFailed(object sender, UnhandledExceptionEventArgs e)
		{
			Log.smethod_5("ChatImgItem", "Loading gif failed");
		}
		private void SetEmptyImage()
		{
			this.image_0 = new Image();
			this.image_0.set_Source(new ImageSourceConverter().ConvertFromString("/Resource/Img/Chat/empty_img_nor.png") as ImageSource);
			this.image_0.set_Width(60.0);
			this.image_0.set_Height(60.0);
			if (this.imageLayout.get_Children().get_Count() > 0)
			{
				this.imageLayout.get_Children().Clear();
			}
			this.imageLayout.get_Children().Add(this.image_0);
		}
		public void showStaticImage()
		{
			if (this.gifControl_0 != null)
			{
				try
				{
					if (this.gifControl_0.IsGif())
					{
						this.gifControl_0.Stop();
						this.gifControl_0.Release();
					}
				}
				catch (Exception)
				{
					Log.smethod_1("ChatImgItem", "showStaticImage Err");
				}
				this.gifControl_0.Source = this.chatMsg_0.strThumbnail;
			}
		}
		public void showGifImage()
		{
			if (this.gifControl_0 != null && !string.IsNullOrEmpty(this.chatMsg_0.strPath))
			{
				this.gifControl_0.Source = this.chatMsg_0.strPath;
			}
		}
		private static bool isGifPic(string imageSource)
		{
			if (string.IsNullOrEmpty(imageSource))
			{
				return false;
			}
			if (imageSource.StartsWith("/Resource"))
			{
				imageSource = imageSource.Substring(1);
			}
			Stream stream;
			if (imageSource.StartsWith("Resource"))
			{
				Uri uri = new Uri(imageSource, 2);
				StreamResourceInfo resourceStream = Application.GetResourceStream(uri);
				if (resourceStream == null)
				{
					return false;
				}
				stream = resourceStream.get_Stream();
			}
			else
			{
				byte[] array = StorageIO.readFromFile(imageSource);
				stream = new MemoryStream(array);
			}
			if (stream == null)
			{
				return false;
			}
			stream.set_Position(0L);
			byte[] array2 = new byte[100];
			stream.Read(array2, 0, 100);
			ImageFromat imageFormat = ImageUtil.getImageFormat(array2);
			stream.set_Position(0L);
			return imageFormat == ImageFromat.const_4;
		}
		public override void SetImgSrc(string ImgSrc)
		{
			this.mImgSrc = ImgSrc;
		}
		public override void SetProgress(int Progress)
		{
			this.mProgress = Progress;
			this.RefreshProgress();
		}
	}
}
